<?php
// +----------------------------------------------------------------------
// |     【CustomerMain】Model模型类
// +----------------------------------------------------------------------

declare (strict_types = 1);

namespace app\common\model;

use think\Model;
use think\model\concern\SoftDelete;

class CustomerMain extends Model{
    use SoftDelete;
    protected $deleteTime = "delete_time";
	 
	/**
	 * 登记一对一关联
	 */
	public function gradeName(){
	    return $this->hasOne(CustomerGrade::class, 'id', 'gid')->bind(['grade'=>'name',]);
	}
	
    // 获取列表
    public static function getList($where = []){
        $limit = I('get.limit/d');
		
		//按品名查找
		if ($title = I("title")) {
			$where[]=['name|phone|company|qq|wangid|province','like','%'. $title .'%'];
		}

        $list = self::order('id','desc')->with(['gradeName'])->append(['address2'])->where($where)->paginate($limit);
        return ['code'=>0,'data'=>$list->items(),'extend'=>['count' => $list->total(), 'limit' => $limit]];
    }
	
	// 获取列表数据
	public static function listData($where=[],$order="",$n_page=0,$limit=20){
		$data=self::order($order)->where($where)->append(['address2']);
		if($n_page>0){
			$data=$data->page($n_page,$limit);
		}
		$data=$data->select()->toArray();
		
		return $data;
	}
	
	// 获取某个单条记录
	public static function info($id){
		if(!is_array($id)){
			$model=self::find($id);
		}else{
			$model=self::where($id)->find();
		}
		return $model;		
	}
	
	/** 获取器 **/
	public function getCreateTimeAttr($value,$data){
		return date('Y-m-d H:i',$data['create_time']);
		return $config[$data['addtype']]['val'];;		
	}
	
	public function getDealtimeAttr($value){
		$value=wordTime($value);
	    return $value;
	}
	
	public function getPhoneAttr($value){
		$value=\core\Encode::decrypt($value);
	    return $value;
	}
	
	public function getAddress2Attr($value,$data){
		$value=$data['province'].$data['city'].$data['area'].$data['address'];
	    return $value;
	}
	
}
